unit u_sim;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, StdCtrls, Spin, Buttons;

type
  TF_sim = class(TForm)
    Label1: TLabel;
    CBLeft: TComboBox;
    CBRight: TComboBox;
    imleft: TImage;
    imright: TImage;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    LaLeft: TLabel;
    LdLeft: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    LdRight: TLabel;
    LaRight: TLabel;
    SeLeft: TSpinEdit;
    Label5: TLabel;
    SERight: TSpinEdit;
    Label6: TLabel;
    TerLeft: TComboBox;
    TerRight: TComboBox;
    Dleft: TSpinEdit;
    Dright: TSpinEdit;
    Batl: TImage;
    BatR: TImage;
    Timer: TTimer;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    okl: TCheckBox;
    okr: TCheckBox;
    cal: TComboBox;
    cdl: TComboBox;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    car: TComboBox;
    cdr: TComboBox;
    Label15: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    cl: TCheckBox;
    cr: TCheckBox;
    Edit1: TEdit;
    Edit2: TEdit;
    expir1: TLabel;
    expir2: TLabel;
    Label14: TLabel;
    Label18: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure CBLeftClick(Sender: TObject);
    procedure CBRightClick(Sender: TObject);
    procedure TerLeftChange(Sender: TObject);
    procedure TerRightChange(Sender: TObject);
    procedure SeLeftChange(Sender: TObject);
    procedure SERightChange(Sender: TObject);
    procedure DleftChange(Sender: TObject);
    procedure DrightChange(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure TimerTimer(Sender: TObject);
    procedure Label4Click(Sender: TObject);
    procedure Label10Click(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure SpeedButton2Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure crClick(Sender: TObject);
    procedure clClick(Sender: TObject);
    procedure calChange(Sender: TObject);
    procedure cdlChange(Sender: TObject);
    procedure carChange(Sender: TObject);
    procedure cdrChange(Sender: TObject);
    procedure oklClick(Sender: TObject);
    procedure okrClick(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

Procedure FillCombo(cb:Tcombobox);
Procedure FillCombo1(cb:Tcombobox);
Function GetAttack(typ,level,terra,dam:byte):real;
Function GetDefence(typ,level,terra,dam:byte):real;
var
  F_sim: TF_sim;
  ar:array[0..5] of byte;
  isl,e1,e2,e3,e4:boolean;
  dam1,dam2:byte;


implementation

uses struct,clunit1,HStruct;
{$R *.DFM}


function getatlev(at:real;lev,lev1:integer):real;
var
 oa:real;
 i:integer;
 ll:integer;
begin
 oa:=at;
 ll:=lev-lev1;
 if ll>0 then begin
  for i:=1 to ll do oa:=oa+(oa/100)*perlev;
 end;
 result:=oa;
end;



Function GEtAttack;
var
 a:real;
 dm:real;
begin
 a:=hconf.cfgs[typ].atack;//+(hconf.cfgs[typ].atack/100)*(level-1)*perlev;
 result:=a;
end;

Function GEtDefence;
var
 a:real;
 dm:word;
begin
 level:=level-1;
 a:=hconf.cfgs[typ].defence;//+(hconf.cfgs[typ].defence/100)*level*perlev;
 result:=a;
end;



Procedure FillCombo;
var
 ind:integer;
 lev:byte;
 ab,db,a1b,d1b:integer;
 dst:integer;
 ast:integer;
 tba,tbd:integer;
 al,dl:real;
begin
 ind:=cb.itemindex;
 lev:=f_sim.seleft.value;
 ast:=0;dst:=0;
 ab:=hconf.GetAtBonU(ind,f_sim.cbright.itemindex);
 db:=hconf.GetDefBonU(ind,f_sim.cbright.itemindex);
 tba:=hconf.GetAtBonT(f_sim.cbleft.itemindex,f_sim.terright.itemindex);
 tbd:=hconf.GetDefBonT(f_sim.cbleft.itemindex,f_sim.terleft.itemindex);
 ast:=ast+ab;
 ast:=ast+tba;
 dst:=dst+db;
 dst:=dst+tbd;
 al:=Getattack(ind,lev,f_sim.TerLeft.ItemIndex,f_sim.dleft.value);
 al:=getatlev(al+ast,lev,f_sim.seright.value);
 dl:=Getdefence(ind,lev,f_sim.TerLeft.ItemIndex,f_sim.dleft.value);
 dl:=getatlev(dl+dst,lev,f_sim.seright.value);
 if f_sim.okl.Checked then dl:=dl+(dl/100)*okopper;
 if f_sim.cl.checked then begin
  al:=al+(al/100)*strtoint(f_sim.cal.text);
  dl:=dl+(dl/100)*strtoint(f_sim.cdl.text);
 end;
 al:=al*damkoof(f_sim.dleft.value,strtoint(f_sim.edit1.text));
 dl:=dl*damkoof(f_sim.dleft.value,strtoint(f_sim.edit1.text));
 f_sim.LaLeft.caption:=floattostrf(al,ffgeneral,4,4);
 f_sim.ldleft.caption:=floattostrf(dl,ffgeneral,4,4);
 f_sim.imleft.Canvas.brush.Color:=clbtnface;
 f_sim.imleft.canvas.fillrect(rect(0,0,29,29));
 if ind<5 then mainwin.il1.draw(f_sim.imleft.canvas,0,0,ar[f_sim.terleft.itemindex]);
 if (ind=5) or (ind=6) then mainwin.il1.draw(f_sim.imleft.canvas,0,0,ar[0]);
 if ind>6 then mainwin.il1.draw(f_sim.imleft.canvas,0,0,0);
 mainwin.ul1.draw(f_sim.imleft.canvas,0,0,ind);
 if ind<5 then mainwin.il1.draw(f_sim.batl.canvas,0,0,ar[f_sim.terleft.itemindex]);
 if (ind=5) or (ind=6) then mainwin.il1.draw(f_sim.batl.canvas,0,0,ar[0]);
 if ind>6 then mainwin.il1.draw(f_sim.batl.canvas,0,0,0);
 mainwin.ul1.draw(f_sim.batl.canvas,0,0,ind);
 f_sim.batl.refresh;
end;

Procedure FillCombo1;
var
 lev,ind:integer;
 ast,dst:integer;
 ab,db:integer;
 tba,tbd:integer;
 arr,dr:real;
begin
 ind:=cb.itemindex;
 lev:=f_sim.seright.value;
 ast:=0;dst:=0;
 ab:=hconf.GetAtBonU(ind,f_sim.cbleft.itemindex);
 db:=hconf.GetDefBonU(ind,f_sim.cbleft.itemindex);
 tba:=hconf.GetAtBonT(f_sim.cbright.itemindex,f_sim.terleft.itemindex);
 tbd:=hconf.GetDefBonT(f_sim.cbright.itemindex,f_sim.terright.itemindex);
 ast:=ast+ab;
 ast:=ast+tba;
 dst:=dst+db;
 dst:=dst+tbd;
 arr:=Getattack(ind,lev,f_sim.Terright.ItemIndex,f_sim.dright.value);
 arr:=getatlev(arr+ast,lev,f_sim.seleft.value);
 dr:=Getdefence(ind,lev,f_sim.Terright.ItemIndex,f_sim.dright.value);
 dr:=getatlev(dr+dst,lev,f_sim.seleft.value);
 if f_sim.okr.Checked then dr:=dr+(dr/100)*okopper;
 if f_sim.cr.checked then begin
  arr:=arr+(arr/100)*strtoint(f_sim.car.text);
  dr:=dr+(dr/100)*strtoint(f_sim.cdr.text);
 end;
 arr:=arr*damkoof(f_sim.dright.value,strtoint(f_sim.edit2.text));
 dr:=dr*damkoof(f_sim.dright.value,strtoint(f_sim.edit2.text));
 f_sim.Laright.caption:=floattostrf(arr,ffgeneral,4,4);
 f_sim.ldright.caption:=floattostrf(dr,ffgeneral,4,4);
 f_sim.imright.Canvas.brush.Color:=clbtnface;
 f_sim.imright.canvas.fillrect(rect(0,0,29,29));
 if ind<5 then mainwin.il1.draw(f_sim.imright.canvas,0,0,ar[f_sim.terright.itemindex]);
 if (ind=5) or (ind=6) then mainwin.il1.draw(f_sim.imright.canvas,0,0,ar[0]);
 if ind>6 then mainwin.il1.draw(f_sim.imright.canvas,0,0,0);
 mainwin.ul1.draw(f_sim.imright.canvas,0,0,ind);

 if ind<5 then mainwin.il1.draw(f_sim.batr.canvas,0,0,ar[f_sim.terright.itemindex]);
 if (ind=5) or (ind=6) then mainwin.il1.draw(f_sim.batr.canvas,0,0,ar[0]);
 if ind>6 then mainwin.il1.draw(f_sim.batr.canvas,0,0,0);
 mainwin.ul1.draw(f_sim.batr.canvas,0,0,ind);
 f_sim.batr.refresh;
end;




procedure TF_sim.FormCreate(Sender: TObject);
var
 i:integer;
begin
 ar[0]:=1;
 ar[1]:=68;
 ar[2]:=46;
 ar[3]:=85;
 ar[4]:=Townn;
 ar[5]:=Mountin1;
 terleft.itemindex:=0;
 terright.itemindex:=0;
 seleft.Text:='1';
 seright.Text:='1';
 if fileexists(curdir+'armies.dat') then hconf.loadfromfile(curdir+'armies.dat');
 for i:=0 to 14 do begin
  cbleft.items.Add(hconf.cfgs[i].name);
  cbright.items.Add(hconf.cfgs[i].name);
 end;
 cbleft.ItemIndex:=0;
 cbright.ItemIndex:=0;
 fillCombo1(cbright);
 fillCombo(cbleft);
end;

procedure TF_sim.CBLeftClick(Sender: TObject);
begin
  FillCombo(cbleft);
  fillCombo1(cbright);
  if cbleft.itemindex>6 then TerLeft.enabled:=false
   else TerLeft.enabled:=true;
end;

procedure TF_sim.CBRightClick(Sender: TObject);
begin
 fillCombo1(cbright);
 fillCombo(cbleft);
 if cbright.itemindex>6 then TerRight.enabled:=false
  else TerRight.enabled:=true;
end;

procedure TF_sim.TerLeftChange(Sender: TObject);
begin
 mainwin.il1.draw(f_sim.imleft.canvas,0,0,ar[terleft.itemindex]);
 mainwin.ul1.draw(f_sim.imleft.canvas,0,0,cbleft.itemindex);
 mainwin.il1.draw(f_sim.batl.canvas,0,0,ar[terleft.itemindex]);
 mainwin.ul1.draw(f_sim.batl.canvas,0,0,cbleft.itemindex);
 f_sim.imleft.Refresh;
 f_sim.Batl.refresh;
  FillCombo(cbleft);
  fillCombo1(cbright);
  if cbleft.itemindex>6 then TerLeft.enabled:=false
   else TerLeft.enabled:=true;
end;

procedure TF_sim.TerRightChange(Sender: TObject);
begin
 mainwin.il1.draw(f_sim.imright.canvas,0,0,ar[terright.itemindex]);
 mainwin.ul1.draw(f_sim.imright.canvas,0,0,cbright.itemindex);
 mainwin.il1.draw(f_sim.batr.canvas,0,0,ar[terright.itemindex]);
 mainwin.ul1.draw(f_sim.batr.canvas,0,0,cbright.itemindex);
 f_sim.imright.Refresh;
 f_sim.BatR.Refresh;
 fillCombo1(cbright);
 fillCombo(cbleft);
 if cbright.itemindex>6 then TerRight.enabled:=false
  else TerRight.enabled:=true;
end;

procedure TF_sim.SeLeftChange(Sender: TObject);
begin
 Fillcombo(cbleft);
 FillCombo1(cbright);
end;

procedure TF_sim.SERightChange(Sender: TObject);
begin
 Fillcombo(cbleft);
 FillCombo1(cbright);
end;

procedure TF_sim.DleftChange(Sender: TObject);
begin
 Fillcombo(cbleft);
 FillCombo1(cbright);
end;

procedure TF_sim.DrightChange(Sender: TObject);
begin
 FillCombo1(cbright);
 Fillcombo(cbleft);
end;

procedure TF_sim.SpeedButton1Click(Sender: TObject);
begin
 isl:=true;
 batl.left:=136;
 batr.left:=192;
 timer.interval:=30;
 timer.enabled:=true;
 e1:=false;e2:=false;e3:=false;e4:=false;
end;

procedure TF_sim.TimerTimer(Sender: TObject);
var
 ind:integer;
 dm,a1,a2,d1,d2:real;
 dam,tba,tbd,ab,db:integer;
begin

 if e4 then begin
  ind:=cbleft.itemindex;
  if ind<5 then mainwin.il1.draw(f_sim.batl.canvas,0,0,ar[f_sim.terleft.itemindex]);
  if (ind=5) or (ind=6) then mainwin.il1.draw(f_sim.batl.canvas,0,0,ar[0]);
  if ind>6 then mainwin.il1.draw(f_sim.batl.canvas,0,0,0);
  mainwin.ul1.draw(f_sim.batl.canvas,0,0,ind);
  f_sim.batl.refresh;
  e4:=false;
  if not isl then e1:=true else begin
   timer.enabled:=false;
  end;
 end;

 if e3 then begin
  a1:=strtofloat(laright.caption);
  d2:=strtofloat(ldleft.caption);
  dam2:=GetFight(a1,d2);
  expir2.caption:=inttostr(strtoint(expir2.caption)+getexpir(a1,d2,dright.value,dleft.value,dam2,1,1));
  dleft.Value:=dleft.value+dam2;
  edit1.text:=inttostr(strtoint(edit1.text)+1);
  FillCombo1(cbright);
  Fillcombo(cbleft);
  ind:=cbleft.itemindex;
  if ind<5 then mainwin.il1.draw(f_sim.batl.canvas,0,0,ar[f_sim.terleft.itemindex]);
  if (ind=5) or (ind=6) then mainwin.il1.draw(f_sim.batl.canvas,0,0,ar[0]);
  if ind>6 then mainwin.il1.draw(f_sim.batl.canvas,0,0,0);
  mainwin.ul1.draw(f_sim.batl.canvas,0,0,ind);
  mainwin.ul1.draw(f_sim.batl.canvas,0,0,explo);
  batl.Canvas.TextOut(5,5,inttostr(dam2));
  f_sim.batl.refresh;
  e3:=false;e4:=true;
 end;


 if e2 then begin
  ind:=cbright.itemindex;
  if ind<5 then mainwin.il1.draw(f_sim.batr.canvas,0,0,ar[f_sim.terright.itemindex]);
  if (ind=5) or (ind=6) then mainwin.il1.draw(f_sim.batr.canvas,0,0,ar[0]);
  if ind>6 then mainwin.il1.draw(f_sim.batr.canvas,0,0,0);
  mainwin.ul1.draw(f_sim.batr.canvas,0,0,ind);
  f_sim.batr.refresh;
  e2:=false;
  if isl then e3:=true else begin
   timer.enabled:=false;
  end;
 end;

 if e1 then begin
  a1:=strtofloat(laleft.caption);
  d2:=strtofloat(ldright.caption);
  dam1:=GetFight(a1,d2);
  expir1.caption:=inttostr(strtoint(expir1.caption)+getexpir(a1,d2,dleft.value,dright.value,dam1,1,1));
  dright.value:=dright.value+dam1;
  edit2.text:=inttostr(strtoint(edit2.text)+1);
  FillCombo1(cbright);
  Fillcombo(cbleft);
  ind:=cbright.itemindex;
  if ind<5 then mainwin.il1.draw(f_sim.batr.canvas,0,0,ar[f_sim.terright.itemindex]);
  if (ind=5) or (ind=6) then mainwin.il1.draw(f_sim.batr.canvas,0,0,ar[0]);
  if ind>6 then mainwin.il1.draw(f_sim.batr.canvas,0,0,0);
  mainwin.ul1.draw(f_sim.batr.canvas,0,0,ind);
  mainwin.ul1.draw(f_sim.batr.canvas,0,0,explo);
  batr.Canvas.TextOut(5,5,inttostr(dam1));
  f_sim.batr.refresh;
  e1:=false;
  e2:=true;
 end;

 if timer.interval=30 then begin
  Batl.left:=batl.left+3;
  Batr.left:=batr.left-3;
 end;
 if (timer.interval=30) and (batr.left=batl.left+32) then begin
  timer.interval:=200;
  if isl then e1:=true else e3:=true;
 end;
end;

procedure TF_sim.Label4Click(Sender: TObject);
begin
 dleft.value:=0;
 edit1.text:='0';
 expir1.caption:='0';
end;

procedure TF_sim.Label10Click(Sender: TObject);
begin
 dright.value:=0;
 edit2.text:='0';
 expir2.caption:='0';
end;

procedure TF_sim.FormKeyPress(Sender: TObject; var Key: Char);
begin
 if key=#27 then begin
  key:=#0;
  close;
 end;
end;

procedure TF_sim.SpeedButton2Click(Sender: TObject);
begin
 formactivate(sender);
 fillCombo1(cbright);
 fillCombo(cbleft);
end;

procedure TF_sim.SpeedButton3Click(Sender: TObject);
begin
 isl:=false;
 batl.left:=136;
 batr.left:=192;
 timer.interval:=30;
 timer.enabled:=true;
 e1:=false;e2:=false;e3:=false;e4:=false;
end;

procedure TF_sim.FormActivate(Sender: TObject);
var i:integer;
begin
   if ispbem then hconf.loadfromfile(pbemdir+'armies.dat')
   else hconf.loadfromfile(curdir+'armies.dat');
   cbleft.items.clear;
   cbright.items.clear;
    for i:=0 to 14 do begin
    cbleft.items.Add(hconf.cfgs[i].name);
    cbright.items.Add(hconf.cfgs[i].name);
    cbleft.ItemIndex := 0;
    cbright.ItemIndex := 0;
   end;

end;

procedure TF_sim.crClick(Sender: TObject);
begin
 car.enabled:=cr.checked;
 cdr.enabled:=car.enabled;
 fillCombo1(cbright);
 fillCombo(cbleft);
end;

procedure TF_sim.clClick(Sender: TObject);
begin
 cal.enabled:=cl.checked;
 cdl.enabled:=cl.checked;
 fillCombo1(cbright);
 fillCombo(cbleft);
end;

procedure TF_sim.calChange(Sender: TObject);
begin
 fillCombo1(cbright);
 fillCombo(cbleft);
end;

procedure TF_sim.cdlChange(Sender: TObject);
begin
fillCombo1(cbright);
 fillCombo(cbleft);
end;

procedure TF_sim.carChange(Sender: TObject);
begin
fillCombo1(cbright);
 fillCombo(cbleft);
end;

procedure TF_sim.cdrChange(Sender: TObject);
begin
fillCombo1(cbright);
 fillCombo(cbleft);
end;

procedure TF_sim.oklClick(Sender: TObject);
begin
fillCombo1(cbright);
 fillCombo(cbleft);
end;

procedure TF_sim.okrClick(Sender: TObject);
begin
fillCombo1(cbright);
 fillCombo(cbleft);
end;

procedure TF_sim.SpeedButton4Click(Sender: TObject);
begin
{ comform.show;}
end;

end.
